import redis
from common.com_conf import Conf


def init():
    pool = redis.ConnectionPool(
        host=Conf.yaml["dpb"]["basic"]["django"]["redis"]["host"],
        port=Conf.yaml["dpb"]["basic"]["django"]["redis"]["port"],
        password=Conf.yaml["dpb"]["basic"]["django"]["redis"]["pwd"],
        max_connections=Conf.yaml["dpb"]["basic"]["django"]["redis"]["max_conn"],
        db=Conf.yaml["dpb"]["basic"]["django"]["redis"]["db"],
        decode_responses=True)

    return pool


class Redis:
    __pool: redis.ConnectionPool = init()

    @classmethod
    def hmset(cls, name, mapping):
        with redis.StrictRedis(connection_pool=cls.__pool) as conn:
            conn.hmset(name=name, mapping=mapping)

    @classmethod
    def get(cls, name):
        with redis.StrictRedis(connection_pool=cls.__pool) as conn:
            return conn.get(name)
